package com.zlsy.secondtime.threadcoreknowledge.threadobjectclasscommonmethods;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author zhouliang
 * @date 2020/5/18 11:31
 */
@Slf4j
public class JoinStateThread {

    // 主线程在等待子线程的时候属于线程的什么状态 ？

    public static void main(String[] args) {
        Thread mainThread = Thread.currentThread();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    log.info("主线程在子线程进行sleep之前为:{}", mainThread.getState());
                    TimeUnit.SECONDS.sleep(3);
                    log.info("主线程在子线程进行sleep之后为:{}", mainThread.getState());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        //子线程启动
        thread.start();
        try {
            //子线程加入主线程，主线程等待子线程执行完的这个过程，主线程的线程状态为什么？ wait
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //子线程执行完了，这时候主线程状态？代码没执行完 运行中
        log.info("{}", mainThread.getState());
    }
}
